package dialog;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zx.theline.R;

import org.apache.http.Header;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import GsonBean.AddPointBean;
import GsonBean.RjhSgxmListBean;
import GsonBean.SuccessBean;
import GsonBean.post.AddPointPost;
import Utils.MyToast;
import app.MyApplication;
import base.BaseDialog;
import greendao.ProtectMeasuresDao;
import network.NetWorkApi;
import network.ObjectHttpResponseHandler;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import views.AddDangerPointTextView;
import views.AddDangerPointView;
import user.LoginUser;

/**
 * Created by zx on 2017/3/21.
 * 补充风险点的弹出框
 */

@SuppressWarnings({"WeakerAccess", "unused"})
public class AddDangerPointDialog extends BaseDialog {
    LoginUser mLoginUser;
    String jihuahao = "";
    LinearLayout yyfxd_layout, bcfxd_layout;
    TextView queding;
    TextView quxiao;
    TextView add;
    AddPointBean mAddPointBean;

    //给选择风险点dialog提供数据， 内容为该项目： 可选择安全风险ID，可选安全风险措施ID；
    List<Integer> mList;

    List<AddDangerPointView> bcfxdList;
    HashMap<String, Integer> mMap;
    ProtectMeasuresDao mProtectMeasuresDao;

    public AddDangerPointDialog(Context context) {
        super(context, R.style.AppTheme);
//        this.mList=mList;
    }

    public AddDangerPointDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    protected AddDangerPointDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_adddangerpoint);
        InitView();
        InitData();
        InitListen();
    }

    @Override
    protected void InitView() {
        yyfxd_layout = (LinearLayout) findViewById(R.id.yyfxd_layout);
        bcfxd_layout = (LinearLayout) findViewById(R.id.bcfxd_layout);
        queding = (TextView) findViewById(R.id.queding);
        quxiao = (TextView) findViewById(R.id.quxiao);
        add = (TextView) findViewById(R.id.add);
    }

    public void InitData() {
        mProtectMeasuresDao = MyApplication.getDaoSession().getProtectMeasuresDao();
        mLoginUser = MyApplication.getmLoginUser();
        bcfxdList=new ArrayList<>();
    }

    @Override
    protected void InitListen() {
        //取消按钮事件
        quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AddDangerPointDialog.this.dismiss();
            }
        });
        //确定按钮事件
        queding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //获取补充风险点内容
                List<AddPointPost> mList = getBcfxd();
                if(mList.size() == 0){
                    Toast.makeText(getContext(),"请选择一个风险点",Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    NetWorkApi.UpdataBcaqfx(mList, new Callback<AddPointPost>() {
                                @Override
                                public void onResponse(Call<AddPointPost> call, Response<AddPointPost> response) {
                                    MyApplication.HideWaitDialog();
                                    AddDangerPointDialog.this.dismiss();
                                }

                                @Override
                                public void onFailure(Call<AddPointPost> call, Throwable t) {
                                    MyToast.showFail();
                                }
                            }
                    );
                }
            }
        });
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AddDangerPointView mAddDangerPointView = new AddDangerPointView(getContext(), mMap);
                bcfxd_layout.addView(mAddDangerPointView);
            }
        });
    }

    //获取bcfxd_layout下 所有新增风险点，做出响应筛选，返回list
    private List<AddPointPost> getBcfxd() {
        List<AddPointPost> mList = new ArrayList<>();
        for (int i = 0; i < bcfxd_layout.getChildCount(); i++) {
            AddDangerPointView mView = (AddDangerPointView) bcfxd_layout.getChildAt(i);
            TextView aqfxnr = (TextView) mView.findViewById(R.id.aqfxnr);
            TextView aqfxcs = (TextView) mView.findViewById(R.id.aqfxcs);
            String s1 = aqfxnr.getText().toString();
            String s2 = aqfxcs.getText().toString();
            s1 = s1.replace(" ", "");
            s2 = s2.replace(" ", "");
            if (!s1.equals("") && !s2.equals("")) {
                AddPointPost mAddPointPost = new AddPointPost();
                mAddPointPost.setAqfxnr(s1);
                mAddPointPost.setAqfxcsnr(s2);
                mAddPointPost.setJdjhbBh(jihuahao);
                mAddPointPost.setLrdwId(mLoginUser.getUserUnitId());
                mAddPointPost.setLrrId(mLoginUser.getUserDisplayName());
                mList.add(mAddPointPost);
            }
        }
        return mList;
    }

    public void setJiHuaHao(String s) {
        this.jihuahao = s;
    }

    public void AddDangerPoint(AddPointBean mAddPointBean) {
        this.mAddPointBean = mAddPointBean;
        List<AddPointBean.ExistingFxdListBean> existingFxdList = mAddPointBean.getExistingFxdList();
        List<AddPointBean.SupplementFxdListBean> supplementFxdList = mAddPointBean.getSupplementFxdList();
        List<RjhSgxmListBean> rjhSgxmList = mAddPointBean.getRjhSgxmList();
        mMap = new HashMap<>();
        for (RjhSgxmListBean bean : rjhSgxmList) {
            mMap.put(bean.getSgxmmc(), bean.getSgxmlbzdId());
        }
        //新建已有风险点内容 view并添加到视图
        if (existingFxdList.size() > 0) {
            AddDangerPointTextView mAddDangerPointTextView = new AddDangerPointTextView(getContext(), existingFxdList);
            mAddDangerPointTextView.scfxd.setVisibility(View.GONE);
            yyfxd_layout.addView(mAddDangerPointTextView);
        }
        for (AddPointBean.SupplementFxdListBean bean : supplementFxdList) {
            AddDangerPointView mAddDangerPointView = new AddDangerPointView(getContext(), mMap);
            mAddDangerPointView.setAqfxData(bean);
            bcfxd_layout.addView(mAddDangerPointView);
            bcfxdList.add(mAddDangerPointView);
        }
    }
        //隐藏增加风险点按钮
    public void hideAdd(){
        add.setVisibility(View.GONE);
        queding.setVisibility(View.GONE);
        for(AddDangerPointView v:bcfxdList){
            v.readOnly();
        }
    }

    //显示增加风险点按钮
    public void showAdd(){
        add.setVisibility(View.VISIBLE);
        queding.setVisibility(View.VISIBLE);
    }
}
